package com.gouldzd.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.gouldzd.dao.PermissionDao;
import com.gouldzd.dao.RoleDao;
import com.gouldzd.dao.UserDao;
import com.gouldzd.pojo.Permission;
import com.gouldzd.pojo.Role;
import com.gouldzd.pojo.User;
import com.gouldzd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/*
 * @Author zhangdan
 * @Description //TODO 用户服务
 * @Date 11:06 2020/5/7
 * @Param
 * @return
 **/
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    //根据用户名查询用户查询数据库获取用户信息和关联角色信息,同时要查询角色相关权限信息
    @Override
    public User findByUsername(String username) {

        User user = userDao.findByUsername(username);//查询用户基本信息,不包含用户的角色
        if(user == null){
            return null;
        }

        //根据用户id查询对应的角色
        Set<Role> roles = roleDao.findByUserId(user.getId());

        for (Role role : roles) {
            Integer roleId = role.getId();
            //根据角色id查询关联的权限
            Set<Permission> permissions = permissionDao.findByRoleId(roleId);
            role.setPermissions(permissions);//让角色关联权限
        }
        user.setRoles(roles);//让用户关联角色

        //根据用户名查询用户的角色和权限详细信息
        /*User user = userDao.findRolesAndPermissionByUsername(username);
        if(user == null){
            return null;
        }*/

        return user;
    }
}
